﻿using System.IO;
using System.Text;
using System.Net;
using System.Xml;

using ECCIT;

namespace ECCIT.Net.SAML2.Protocols
{
    public class Status
    {
        // Fields
        private StatusCode statusCode;
        private StatusDetail statusDetail;
        private StatusMessage statusMessage;

        // Properties
        public StatusCode StatusCode
        {
            get { return this.statusCode; }
            set { this.statusCode = value; }
        }

        public StatusDetail StatusDetail
        {
            get { return this.statusDetail; }
            set { this.statusDetail = value; }
        }

        public StatusMessage StatusMessage
        {
            get { return this.statusMessage; }
            set { this.statusMessage = value; }
        }

        // Methods
        public Status()
        {
        }

        public Status(XmlElement xmlElement)
        {
            if (!IsValid(xmlElement))
            {
                throw new SerializationException("The XML is not a Status.");
            }
            foreach (XmlNode node in xmlElement.ChildNodes)
            {
                if (node is XmlElement)
                {
                    XmlElement element = (XmlElement)node;
                    if (StatusCode.IsValid(element))
                    {
                        this.statusCode = new StatusCode(element);
                    }
                    else
                    {
                        if (StatusMessage.IsValid(element))
                        {
                            this.statusMessage = new StatusMessage(element);
                            continue;
                        }
                        if (StatusDetail.IsValid(element))
                        {
                            this.statusDetail = new StatusDetail(element);
                        }
                    }
                }
            }
        }

        public Status(string statusCode, string statusMessage)
        {
            this.statusCode = new StatusCode(statusCode);
            if (!string.IsNullOrEmpty(statusMessage))
            {
                this.statusMessage = new StatusMessage(statusMessage);
            }
        }

        public Status(string statusCode, string subordinateStatusCode, string statusMessage)
        {
            this.statusCode = new StatusCode(statusCode);
            if (!string.IsNullOrEmpty(subordinateStatusCode))
            {
                this.statusCode.SubordinateStatusCode = new StatusCode(subordinateStatusCode);
            }
            if (!string.IsNullOrEmpty(statusMessage))
            {
                this.statusMessage = new StatusMessage(statusMessage);
            }
        }

        public static bool IsValid(XmlElement xmlElement)
        {
            return (xmlElement.LocalName.Equals("Status") && xmlElement.NamespaceURI.Equals("urn:oasis:names:tc:SAML:2.0:protocol"));
        }

        public XmlElement ToXml(XmlDocument xmlDocument)
        {
            XmlElement element = xmlDocument.CreateElement("samlp", "Status", "urn:oasis:names:tc:SAML:2.0:protocol");
            if (this.statusCode != null)
            {
                element.AppendChild(this.statusCode.ToXml(xmlDocument));
            }
            if (this.statusMessage != null)
            {
                element.AppendChild(this.statusMessage.ToXml(xmlDocument));
            }
            if (this.statusDetail != null)
            {
                element.AppendChild(this.statusDetail.ToXml(xmlDocument));
            }
            return element;
        }
    }
}
